package com.zx.dsptuneup_TriMic.ProtocolTools;


import android.util.Log;
import com.zx.dsptuneup_TriMic.App;
import com.zx.dsptuneup_TriMic.Tools.NumberBaseConversionUtil;
import com.zx.dsptuneup_TriMic.Tools.TenValueToHexUtils;
import com.zx.dsptuneup_TriMic.Util.ByteToString;
import com.zx.dsptuneup_TriMic.Util.Constants;
import java.util.Random;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;

/**
 * 协议发送，处理类
 */
public class ProtocolConversion {
    private static final String TAG = "ProtocolConversion";

    /**
     * Gets frame id.
     * 获取帧ID   u16  2个字节的随机数(最大65535)
     *
     * @return the frame id
     */
    public static String getFrameId() {
        byte [] b=new byte[2];
        Random random=new Random();
        random.nextBytes(b);
        String hexStr = ByteToString.bytesToHexString(b);
//        int num = NumberBaseConversionUtil.SixToTen(hexStr);
//        Log.e("随机数","num=="+num);
//        Log.e("随机数","hexStr=="+hexStr);
        return hexStr;
    }

    /**
     * 查询指令
     *  head + data_length + crc + frame_id + operation_type + element_type + tail
     * */
    public void  setQUERY(String element_type) {

        String frameId = getFrameId();
        String operation = Constants.operation_typeEnum.get_device;

        String queryProtocol =  getProtocol(frameId,operation,element_type,"");
        Log.e(TAG,"protocol=="+queryProtocol);
//        App.getContext().getNettyClient().sendMsgToServer(queryProtocol, new ChannelFutureListener() {
//            @Override
//            public void operationComplete(ChannelFuture channelFuture) throws Exception {
//                if (channelFuture.isSuccess()) {                //4
////                    Log.d(TAG, queryProtocol + "Write auth successful");
//                } else {
//                    Log.d(TAG, "Write auth error");
//                }
//            }
//        });
    }


    /**
     *  拼接用户基础协议
     *  head + data_length + crc + frame_id + operation_type + element_type + element_content + tail
     * */
    private static String getProtocol(String frameId,String operation,String element_type,String data) {
        int len = data.length() / 2;

        StringBuffer stringBuffer=new StringBuffer();
        stringBuffer.append(Constants.protocolEnum.head);
        stringBuffer.append(TenValueToHexUtils.TenToHexAndAppendZeroTo4(len+4));
        stringBuffer.append("0000");  //crc

        stringBuffer.append(frameId);
        stringBuffer.append(operation);
        stringBuffer.append(element_type);
        stringBuffer.append(data).append(Constants.protocolEnum.tail);

        Log.e("getProtocol","数据长度=="+TenValueToHexUtils.TenToHexAndAppendZeroTo4(len+4));
        Log.e("getProtocol","操作类型+element_type=="+operation+element_type);
        return stringBuffer.toString();
    }


    /**
     * Sets eq.
     * type(u8)      0000
     * frequent(u16)  20-20k
     * Q(u16)         0-1000（值0.0-10.00）
     * Gain(u16)      -7200 - 10000 (-72 - 10)
     *
     * @param frameId       the frame id
     * @param operation     the operation
     * @param element_type  输入 输出 音频矩阵路由 高低音调节 互斥命令
     * @param channel_index the channel index 通道
     * @param segment_index 调节那一段EQ
     * @param frequent      the frequent
     * @param q_value       the q value
     * @param gain          the gain
     * @return the eq
     */
    public static void setEQ(String frameId,String operation,String element_type,int channel_index,int segment_index,int frequent,double q_value,double gain) {

        StringBuffer stringBuffer=new StringBuffer();
        stringBuffer.append(TenValueToHexUtils.TenToHexAndAppendZeroTo2(channel_index));
        stringBuffer.append(Constants.TypeEnum.EQ);
        stringBuffer.append(TenValueToHexUtils.TenToHexAndAppendZeroTo2(segment_index));

        stringBuffer.append("00");   //type
        stringBuffer.append(TenValueToHexUtils.TenToHexAndAppendZeroTo4(frequent));
        stringBuffer.append(NumberBaseConversionUtil.tenDecimalToSixTeen(q_value));
        stringBuffer.append(NumberBaseConversionUtil.tenDecimalToSixTeen(gain));
        Log.d(TAG,"data == "+stringBuffer.toString());

        String protocol =  getProtocol(frameId,operation,element_type,stringBuffer.toString());
        Log.d(TAG,"protocol=="+protocol);

        Log.e(TAG,"EQ指令=通道=="+channel_index+"  eq段=="+segment_index+"  频率=="+frequent+"  q=="+q_value+"  gain=="+gain);

//        App.getContext().getNettyClient().sendMsgToServer(protocol, new ChannelFutureListener() {
//            @Override
//            public void operationComplete(ChannelFuture channelFuture) throws Exception {
//                if (channelFuture.isSuccess()) {                //4
////                    Log.d(TAG, "Write auth successful");
//                } else {
//                    Log.d(TAG, "Write auth error");
//                }
//            }
//        });
    }


    /**
     * Sets mute.  静音指令
     * mute_tatus
     * 00 静音关闭  按钮未选中
     * 01 静音开启  按钮选中
     */
    public static void setMute(String frameId,String operation,String element_type,int channel_index,boolean mute_tatus) {
        StringBuffer stringBuffer=new StringBuffer();
        stringBuffer.append(TenValueToHexUtils.TenToHexAndAppendZeroTo2(channel_index));
        stringBuffer.append(Constants.TypeEnum.MUTE);
        stringBuffer.append(mute_tatus ? "01" : "00");

        Log.d(TAG,"data == "+stringBuffer.toString());

        String protocol =  getProtocol(frameId,operation,element_type,stringBuffer.toString());
        Log.d(TAG,"protocol=="+protocol);

        Log.e(TAG,"静音指令=通道=="+channel_index+"静音="+ mute_tatus );
//        App.getContext().getNettyClient().sendMsgToServer(protocol, new ChannelFutureListener() {
//            @Override
//            public void operationComplete(ChannelFuture channelFuture) throws Exception {
//                if (channelFuture.isSuccess()) {                //4
////                    Log.d(TAG, "Write auth successful");
//                } else {
//                    Log.d(TAG, "Write auth error");
//                }
//            }
//        });
    }


    /**
     * Sets volume.
     * 普通用户只调节音量(增益 gain)
     */
    public static void setVolume(String frameId,String operation,String element_type,int channel_index,double volume) {
        StringBuffer stringBuffer=new StringBuffer();
        stringBuffer.append(TenValueToHexUtils.TenToHexAndAppendZeroTo2(channel_index));
        stringBuffer.append(Constants.TypeEnum.VOLUME);
        stringBuffer.append(NumberBaseConversionUtil.tenDecimalToSixTeen(volume) );

        Log.d(TAG,"data == "+stringBuffer.toString());

        String protocol =  getProtocol(frameId,operation,element_type,stringBuffer.toString());
        Log.d(TAG,"protocol=="+protocol);


        Log.e(TAG,"音量指令=通道=="+channel_index+"音量="+ volume);
//        App.getContext().getNettyClient().sendMsgToServer(protocol, new ChannelFutureListener() {
//            @Override
//            public void operationComplete(ChannelFuture channelFuture) throws Exception {
//                if (channelFuture.isSuccess()) {                //4
////                    Log.d(TAG, "Write auth successful");
//                } else {
//                    Log.d(TAG, "Write auth error");
//                }
//            }
//        });

    }


    /**
     * Sets HighLowCut.
     * 高低切状态以及频率
     */
    public static void setHighLowCut(String frameId, String operation, String element_type, int channel_index, String cutSta,int frequent) {
        StringBuffer stringBuffer=new StringBuffer();
        stringBuffer.append(TenValueToHexUtils.TenToHexAndAppendZeroTo2(channel_index));
        stringBuffer.append(Constants.TypeEnum.SWITCH);
        stringBuffer.append(cutSta);
        stringBuffer.append(TenValueToHexUtils.TenToHexAndAppendZeroTo4(frequent));

        Log.d(TAG,"data == "+stringBuffer.toString());

        String protocol =  getProtocol(frameId,operation,element_type,stringBuffer.toString());
        Log.d(TAG,"protocol=="+protocol);


        String tem =  cutSta.equals(Constants.highLowCutEnum.high) ? "高切" : "低切";
        Log.e(TAG,"高低切指令=通道=="+channel_index+ " " + tem);

//        App.getContext().getNettyClient().sendMsgToServer(protocol, new ChannelFutureListener() {
//            @Override
//            public void operationComplete(ChannelFuture channelFuture) throws Exception {
//                if (channelFuture.isSuccess()) {                //4
////                    Log.d(TAG, "Write auth successful");
//                } else {
//                    Log.d(TAG, "Write auth error");
//                }
//            }
//        });

    }

    /**
     * Sets MuteXmose.
     * 麦克风互斥 //远程会议开启
     * 0x00	互斥命令关闭   按钮选中
     * 0x01	互斥命令开启   按钮未选中
     */
    public static void setMuteXmose(String frameId, String operation, String element_type, boolean mute_xmose_sta) {

        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(mute_xmose_sta ? "00" : "01");

        Log.d(TAG,"data == "+stringBuffer.toString());

        String protocol =  getProtocol(frameId,operation,element_type,stringBuffer.toString());
        Log.d(TAG,"protocol=="+protocol);

        String temp = mute_xmose_sta ? "关闭" : "开启";
        Log.e(TAG,"麦克风互斥===" + temp);
//        App.getContext().getNettyClient().sendMsgToServer(protocol, new ChannelFutureListener() {
//            @Override
//            public void operationComplete(ChannelFuture channelFuture) throws Exception {
//                if (channelFuture.isSuccess()) {                //4
////                    Log.d(TAG, "Write auth successful");
//                } else {
//                    Log.d(TAG, "Write auth error");
//                }
//            }
//        });

    }



}
